//保存问卷答案
function saveAnswer(type, url, questionnaireId, customerId, projectId, edit){
	//初始化题目颜色
	initialize();
	var flag = false;
	//定义数组集合，用于往后台传输数据
	var array = new Array();
	//从最顶级回去全部li
	$(".qs-list li").each(function() {
		var obj = $(this);
		if(!getQuestion(obj)){
			flag = true;
		}else{
			//将单个问题内容组装成josn,并存入数组
			array.push(getQuestion(obj));
		}
	});
	//判断必答题是否全部作答
	if(flag){
		layer.msg("有必答题未填写答案，请填写完成后再提交");
		return false;
	}

	var loadIndex = layer.load(1, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
	//保存问题
	$.ajax({
		type: 'post',
        url: url,
        data:{
            "questionnaireId": questionnaireId,
            "customerId": customerId,
            "projectId": projectId,
			"json": JSON.stringify(array),
			"edit": edit
        },
        dataType: 'json',
        //async: false,
        success: function(data){
        	layer.closeAll('loading'); //关闭加载层
        	if(data.res == "1"){
        		layer.msg("问卷保存成功");
        		isSubmit = false;
        		//当选择保存并退出时，调用返回列表方法
        		if(type == "2"){
        			setTimeout("backCustomer()", 2000);
        		}
        	}else{
        		layer.msg(data.resMsg);
        	}
        }
	});
}
//保存问卷时获取问题内容
function getQuestion(obj){
	var flag = false;
	//获取当前是否必填
	var isRequired = obj.attr("isRequired");
	//获取当前问题的ID
	var questionId = obj.attr("questionId");
	//获取当前问题的类型
	var answerType = obj.attr("answerType");
	//获答案ID、答案文本、"其他答案"ID
	var answerId = "",  answerText = "", select_other = "";
	if(answerType == "1"){
		//获取单选框
		answerId = $('input:radio[name="radioAnswer_'+questionId+'"]:checked').val();
		if(answerId == undefined){
			answerId = "";
		}
		//判断必答题是否选择了答案
		if(isRequired == "1" && answerId == ""){
			$("#h3_"+questionId).attr("style", "color:red");
			flag = true;
		}
		//当选择答案为“其他”时，获取填写的文本答案
		if($('input:radio[name="radioAnswer_'+questionId+'"]:checked').attr("at") == "select_other"){
			select_other = answerId;
			answerText = $("#input_"+questionId).val();
			//判断必答题选择"其他"选项是是否填写了答案
			if(isRequired == "1" && answerText == ""){
				$("#h3_"+questionId).attr("style", "color:red");
				flag = true;
			}
		}
	}else if(answerType == "2"){
		//获取复选框
		$("input[name='checkAnswer_"+questionId+"']").each(function(){
			if($(this).is(':checked')){
		    	answerId += '"' + $(this).attr("value") + '",';	
		    	//当选择答案为“其他”时，获取填写的文本答案
				if($(this).attr("at") == "select_other"){
					select_other = $(this).attr("value");
					answerText = $("#input_"+questionId).val();
					//判断必答题选择"其他"选项是是否填写了答案
					if(isRequired == "1" && answerText == ""){
						$("#h3_"+questionId).attr("style", "color:red");
						flag = true;
					}
				}
		   	}
		});
		//将答案集合组装成数组格式
		if(answerId != ""){
			answerId =  '[' + answerId.substring(0, answerId.length-1) + ']';
		}else if(isRequired == "1"){
			//当答案为空时，判断是否必填
			$("#h3_"+questionId).attr("style", "color:red");
			flag = true;
		}
	}else if(answerType == "3"){
		//获取文本答案
		answerText = $("#input_"+questionId).val();
		//判断必答题是否填写了答案
		if(isRequired == "1" && answerText == ""){
			$("#h3_"+questionId).attr("style", "color:red");
			flag = true;
		}
	}
	
	//当有必填答案未填时，直接退出方法
	if(flag){
		return false;
	}
    
    //组装json
    var json = {
    	"questionId": questionId,
    	"answerType": answerType,
    	"answerId": answerId,
    	"select_other": select_other,
    	"answerText": answerText
    };
    return json;
}
//初始化题目颜色
function initialize(){
	$("h3").each(function(){
		$(this).attr("style", "");
	});
}
//判断是否直接退出问卷
function isConfirmBack(){
	if(isSubmit){
		//询问框
		layer.confirm("当前页面尚未保存，是否保存后再退出问卷 ", {
		  	btn: ['是','否'] //按钮
		}, function(){
			//layer.closeAll('dialog');
			save("2");
		}, function(){
			backCustomer();
		});
	}else{
		backCustomer();
	}
}

//获取答案数据
function getAnswer(questionnaireId, customerId){
	$.ajax({
		type: 'post',
        url: jypath+"/customer/customerAnswer/getAnswer",
        data:{
            "projectQuestionnaireId": questionnaireId,
            "customerId": customerId
        },
        dataType: 'json',
        success: function(data){
        	console.log(data);
        	if(data.res != "1"){
        		return false;
        	}
        	setAnswer(data.obj.entity);	
	    }
	});
}
//将数据赋值给对应问题
function setAnswer(data){
	for(var i = 0; i < data.length; i++){
		var info = data[i];
		//问题ID
		var questionId = info.customerProjectQuestionId;
		//选项答案ID
		var answerId = info.customerProjectAnswerId;
		//文本答案（或者“其他”选项文本）
		var answerText = info.answerValue;
		//答案类型
		var answerType = info.answerType;
		if(answerType == "1"){
			$('input:radio[name="radioAnswer_'+questionId+'"]').each(function(){
				if($(this).val() == answerId){
					$(this).attr("checked", true);
				}
			});
		}else if(answerType == "2"){
			$('input[name="checkAnswer_'+questionId+'"]').each(function(){
				if($(this).val() == answerId){
					$(this).attr("checked", true);
				}
			});
		}
		if(answerText != ""){
			$("#input_"+questionId).val(answerText);
		}
	}
}